<?php 

	define('LFY_PATH', __DIR__);
	
	define('LFY_NAMESPACE_PATH', dirname(LFY_PATH));

	if(!defined('LFY_CLI_MODE')) 
		define('LFY_CLI_MODE', false);

	require LFY_PATH.'/funcs.php';
	
	// always do this, if just for a larf
	define('LFY_TIME_IN', \lfy\utime());

	// spl_autoload
	\spl_autoload_register('\\lfy\\autoload');

	// now tell php we've got it covered
	\set_error_handler(array('\\lfy\\error', 'handle'));

	// 3 events fired on shutdown
	\register_shutdown_function(function(){
		\lfy\fire_event('eof'); // end of output
		\lfy\fire_event('shutdown'); // shutdown procedure
		\lfy\fire_event('exit'); // last gasp
	});
	
	// load project config
	\lfy\loadconfig();
	
	// cli mode?
	
	if(LFY_CLI_MODE){

		// lfy dir-prog [args...]
		$script = array_shift($GLOBALS['argv']);
		$target = array_shift($GLOBALS['argv']);
		array_unshift($GLOBALS['argv'], $script);
		
		if($target){
			$a = explode('-', $target);
			if($which = array_pop($a)){
				$class = (count($a) ? '\\' . implode('\\', $a) : '') . "\\bin\\$which";
				if(class_exists($class)){
					$class::initialize();
					$class::main();
					exit(0);
				}
			}
		}
		
		// else ...
		trigger_error("Failed to find target script of '$target'", E_USER_ERROR);

	}
	
	// otherwise ...

	define('LFY_REQUEST', \lfy\strip_request_uri($_SERVER['REQUEST_URI']));

	if($inc = \lfy\getenv('LFY_INCLUDE')){  // include set in env by webserver
		
		require ( \lfy\project_file($inc) );
		
		if(\lfy\getenv('LFY_EXIT'))  // we can exit before caller file takes over
		
			exit(0);
	
	}
	
	// done.
